﻿define(function (require) {

    var DateView = require('views/date.view');
    var UpdateView = require('views/update.view');
    var NotificationView = require('views/notification.view');
    var utility = require('utility/utility.js');

    return Backbone.View.extend({
        tagName: "ul",
        events: {
    },

    initialize: function () {
        $(this.el).addClass('feed-list ui-listview')
                  .attr({
                      "data-role": "listview",
                      "data-split-theme": "d"
                  });


        _.bindAll(this, 'addOne', 'render');
        this.model.bind('getAll', this.render);
    },
    date: null,
    render: function () {
        this.date = null;
        this.model.each(this.addOne);
        E.log("feed list render");
    },
    addEl: function (view) {
        var element = view.render().el;
        $(this.el).append(element);
    },
    addOne: function (e) {
        var _feedListView = this;
        var currentDay = utility.GetCurrentDay(e.get("UpdateOn"));

        if (e.get('Type') == "notification") {
            var view = new NotificationView({ model: e });
            this.addEl(view);
        } else if (e.get('Type') == "update") {

            if (this.date != currentDay) {
                var view = new DateView({ model: e });
                this.addEl(view);
                this.date = currentDay;
            }

            var view = new UpdateView({ model: e });
            this.addEl(view);
        }
    }
});

});